/**********************************
 * @Author: Ronnie Zhang
 * @LastEditor: Ronnie Zhang
 * @LastEditTime: 2023/12/07 20:30:13
 * @Email: zclzone@outlook.com
 * Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
 **********************************/

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as session from 'express-session';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use(
    session({
      secret: 'isme',
      name: 'isme.session',
      rolling: true,
      cookie: { maxAge: null },
      resave: false,
      saveUninitialized: true,
    }),
  );

  // Swagger配置
  const config = new DocumentBuilder()
    .setTitle('RWA Admin Backend API')
    .setDescription('RWA管理系统后端API文档')
    .setVersion('1.0')
    .addTag('认证', '用户认证相关接口')
    .addTag('用户管理', '用户管理相关接口')
    .addTag('角色管理', '角色管理相关接口')
    .addTag('权限管理', '权限管理相关接口')
    .addTag('MongoDB', 'MongoDB数据库操作接口')
    .addBearerAuth(
      {
        type: 'http',
        scheme: 'bearer',
        bearerFormat: 'JWT',
        name: 'JWT',
        description: '输入JWT Token',
        in: 'header',
      },
      'JWT-auth',
    )
    .build();

  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api-docs', app, document, {
    swaggerOptions: {
      persistAuthorization: true,
    },
    customSiteTitle: 'RWA Admin API Documentation',
  });

  const port = process.env.APP_PORT || 8085;
  await app.listen(port);

  console.log(`🚀 启动成功: http://localhost:${port}`);
  console.log(`📚 API文档: http://localhost:${port}/api-docs`);
}
bootstrap();
